package com.woniu.outpatient.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.woniu.outpatient.domain.Drug;
import com.woniu.outpatient.service.IDrugService;

import com.woniu.outpatient.vo.DrugListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniu.outpatient.domain.Druglist;
import com.woniu.outpatient.service.IDruglistService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 药单详情表Controller
 *
 * @author wolf-six
 * @date 2023-02-15
 */
@RestController
@RequestMapping("/druglist")
public class DruglistController extends BaseController
{
    @Autowired
    private IDruglistService druglistService;

    @Autowired
    private IDrugService drugService;

    /**
     * 查询药单详情表列表
     */
    @RequiresPermissions("outpatient:druglist:list")
    @GetMapping("/list")
    public TableDataInfo list(Druglist druglist)
    {
        startPage();

        List<Druglist> list = druglistService.selectDruglistList(druglist);

        for (Druglist druglist1 : list) {
            Long drugId = druglist1.getDrugId();
            Drug drug1 = drugService.selectDrugByDrugId(drugId);
            druglist1.setDrugName(drug1.getDrugName());
        }


        return getDataTable(list);
    }

    /**
     * 查询药单详情表列表
     */
    @RequiresPermissions("outpatient:druglist:AllList")
    @GetMapping("/AllList")
    public TableDataInfo AllList(Druglist druglist)
    {
        startPage();
        List<DrugListVO> list = druglistService.selectAllDruglistList();
        return getDataTable(list);
    }

    /**
     * 导出药单详情表列表
     */
    @RequiresPermissions("outpatient:druglist:export")
    @Log(title = "药单详情表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Druglist druglist)
    {
        List<Druglist> list = druglistService.selectDruglistList(druglist);
        ExcelUtil<Druglist> util = new ExcelUtil<Druglist>(Druglist.class);
        util.exportExcel(response, list, "药单详情表数据");
    }

    /**
     * 获取药单详情表详细信息
     */
    @RequiresPermissions("outpatient:druglist:query")
    @GetMapping(value = "/{druglistId}")
    public AjaxResult getInfo(@PathVariable("druglistId") Long druglistId)
    {
        Druglist druglist = druglistService.selectDruglistByDruglistId(druglistId);
        Long drugId = druglist.getDrugId();
        Drug drug = drugService.selectDrugByDrugId(drugId);
        druglist.setDrugName(drug.getDrugName());
        return success(druglist);
    }

    /**
     * 新增药单详情表
     */
    @RequiresPermissions("outpatient:druglist:add")
    @Log(title = "药单详情表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Druglist druglist)
    {
            return toAjax(druglistService.insertDruglist(druglist));
    }

    /**
     * 修改药单详情表
     */
    @RequiresPermissions("outpatient:druglist:edit")
    @Log(title = "药单详情表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Druglist druglist)
    {
        return toAjax(druglistService.updateDruglist(druglist));
    }

    /**
     * 删除药单详情表
     */
    @RequiresPermissions("outpatient:druglist:remove")
    @Log(title = "药单详情表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{druglistIds}")
    public AjaxResult remove(@PathVariable Long[] druglistIds)
    {
        return toAjax(druglistService.deleteDruglistByDruglistIds(druglistIds));
    }
}
